package net.hn.hnms.sys.message.controller;


import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.sys.common.core.exception.ServiceException;
import net.hn.hnms.sys.common.mail.utils.MailUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api(value = "邮件控制器", tags = {"邮件管理"})
@RestController
@RequestMapping("/mail")
public class MailController {

    /**
     * 发送邮件
     *
     * @param to      接收人
     * @param subject 标题
     * @param text    内容
     */
    @ApiOperation("发送文本邮件")
    @PostMapping("send")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "to", value = "邮件接收人,可以使用逗号“,”分隔，也可以通过分号“;”分隔", required = true, paramType = "query",
                    dataType = "String"),
            @ApiImplicitParam(name = "subject", value = "标题", required = true, paramType = "query",
                    dataType = "String"),
            @ApiImplicitParam(name = "text", value = "邮件内容", required = false, paramType = "query",
                    dataType = "String")
    })
    public String send(String to, String subject, String text) throws ServiceException {
       return MailUtils.sendText(to, subject, text);
    }

    /**
     * 发送文本邮件带附件
     *
     * @param to       接收人
     * @param subject  标题
     * @param text     内容
     * @param fileList 附件
     */
    @ApiOperation("发送文本邮件带附件!!多附件上传请使用postman测试,swagger不支持在一个参数中上传文件数组，如果上传就会传递空值")
    @PostMapping("sendWithAttachment")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "to", value = "邮件接收人", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subject", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "text", value = "邮件内容", required = false, paramType = "query", dataType = "String"),
    })
    public String sendWithAttachment(String to, String subject, String text, @RequestParam(value = "fileList",required = false) MultipartFile[] fileList) throws ServiceException {
        // MultipartFile[] 转成 File List
        List<File> files = null;
        if (!ArrayUtil.isEmpty(fileList)) {
            files = transferToFileList(fileList);
        } else {
            files = new ArrayList<>();
        }
        return MailUtils.sendText(to, subject, text, ArrayUtil.toArray(files, File.class));
    }


    @ApiOperation("多人发送文本邮件带附件!!多附件上传请使用postman测试,swagger不支持在一个参数中上传文件数组，如果上传就会传递空值")
    @PostMapping("sendWithAttachments")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "to", value = "收件人，可以使用逗号“,”分隔，也可以通过分号“;”分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "cc", value = "抄送人，可以使用逗号“,”分隔，也可以通过分号“;”分隔", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "bcc", value = "密送人，可以使用逗号“,”分隔，也可以通过分号“;”分隔", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subject", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "text", value = "邮件内容", required = false, paramType = "query", dataType = "String"),
    })
    public String sendWithAttachments(String to,String cc,String bcc,String subject, String text, @RequestParam(value = "fileList",required = false) MultipartFile[] fileList) throws ServiceException {
        // MultipartFile[] 转成 File List
        List<File> files = null;
        if (!ArrayUtil.isEmpty(fileList)) {
            files = transferToFileList(fileList);
        } else {
            files = new ArrayList<>();
        }
        return MailUtils.send(to, cc, bcc, subject, text, false, ArrayUtil.toArray(files, File.class));
    }




    /**
     * 功能描述: MulipartFile转成File
     *
     * @param multipartFile
     * @return File
     * @auther: WangLong
     * @date: 2022/7/15 13:41
     */
    public final static File transferToFile(MultipartFile multipartFile) {
        //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            //获取源文件名称
            String originalFilename = multipartFile.getOriginalFilename();
            //获取文件后缀
            String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //String[] filename = originalFilename.split("\\.");
            // file = File.createTempFile(filename[0], filename[1]);    //注意下面的 特别注意！！！
            file = File.createTempFile(originalFilename, prefix);    //创建临时文件文件 并且写入file
            multipartFile.transferTo(file);
            //文件如果存在就删除,并且!!!!JVM终止时才真正调用删除操作。即该方法是将删除的命令缓存了一下，到服务停止的时候再进行操作！
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 功能描述: MultipartFile[] 转成 File List
     * @auther: WangLong
     * @date: 2022/7/15 16:22
     * @param fileList
     * @return java.util.List<java.io.File>
     */

    public  List<File> transferToFileList(MultipartFile[] fileList) {
        List<File> files = new ArrayList<>();
        for (MultipartFile multipartFile : fileList) {
            // 调用转换方法
            File file = transferToFile(multipartFile);
            files.add(file);
        }
        return files;
    }



}
